package cc.sunwl.study.java8.时间API;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

public class CastTest {
    public static void main(String[] args) {
        // Date和Instant互相转换
        Date date = Date.from(Instant.now());
        Instant instant = date.toInstant();
        System.out.println("instant: " + instant);
        // Date转换为LocalDateTime
        LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
        System.out.println(localDateTime);
        // LocalDateTime转Date
        Date date1 = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
        System.out.println("date1: " + date1);
        // LocalDate转Date
        Date date2 = Date.from(LocalDate.now().atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
        System.out.println("date2: " + date2);
    }
}
